/* iterate through all items on the page and 
	add them to the rtc with view-interaction */
$(".ProductOfferBox").each(function(i, obj) {
    objrtc(obj,0);
});

/* get the rtc offers for the sidebarbox */
$("#productsidebarboxcontent").load("/rt");

function objrtc(obj,iact){
    var classList = obj.className.split(/\s+/);
	for (var i = 0; i < classList.length; i++) {
	   if (classList[i].substring(0,16) === 'productbox_') {
	     var prodid = classList[i].substring(16);
	     if(prodid.length==10){
	     	rtc(prodid,iact);
	     }
	   }
	}
}

/* this is the onsite retargeting cookie method 
	that collects hovering and clicks onto products 
	and product relevant items which are stored 
	in a cookie and used by the server to determine 
	product offerings. 

	prod = the ASIN of the product interacted with
	iact = type of interaction (0 = view, 1 = hover, 5 = click)
*/
function rtc(prod,iact){
	if($.cookie("rtc")){
		// rtc cookie exists already
		var rtcval = $.cookie('rtc');
		var prodList = rtcval.split('|');
		var prodExists = false;
		for(var p=0;p<prodList.length;p++){
			var proditem = prodList[p].split(':');
			if(proditem[0]==prod){
				// this is the product
				prodExists = true;
 				var iactVal = parseInt(proditem[1])+parseInt(iact);
				rtcval = rtcval.replace(prodList[p],prod+':'+iactVal);
			}
		}

		if(!prodExists){
			rtcval += '|'+prod+':'+iact;
		}

		// update the rtc cookie
		$.cookie('rtc', rtcval, { expires: 90 });
	}else{
		// rtc cookie does not yet exist
		$.cookie('rtc', prod+':'+iact, { expires: 90 });
	}
}